#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/mount.h>
#include <stdlib.h>

typedef struct sockaddr SockAddr;
typedef struct sockaddr_un SockAddrUn;

void do_mount(void);

int main(){
  int sock;
  SockAddrUn addr;
  socklen_t addrlen;
  char buf[1024];
  int n;
  char const *unix_socket_file = "/tmp/afu_dgram";
  sock = socket(AF_UNIX, SOCK_DGRAM, 0);

  addr.sun_family = AF_UNIX;
  strcpy(addr.sun_path, unix_socket_file);
  remove(unix_socket_file);
  bind(sock, (SockAddr*)&addr, sizeof(addr));

  while(1){
    memset(buf, 0, sizeof(buf));
    n = recv(sock, buf, sizeof(buf) - 1, 0);
    printf("recv:%s\n", buf);
	if(n>2 && strncmp(buf, "add", 3) == 0) {
		buf[n] = 0;
        buf[0] = 0;
		do_mount();
	}
  }

  close(sock);
  return 0;
}


void do_mount(void) {
	if(mount("/dev/sdb2", "/mnt/usb/", "ext4", 0, NULL)) {
        perror("mount failed");
        //system("mount /dev/sdb2 /mnt/usb/");
    }
    
    
}
